﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using CoDbObject;
using SZJT.Utility.DataSource;
using System.Data;

namespace Colligate.DataSource.DriverTraining.Business.Coach
{
    class DataNode_BusinessCoachSchedule : BaseDataTree
    {
        private const string DATATYPE_COACHSCHEDULE_INFO = "BizCoachScheduleInfo";
        private const string DATATYPE_COACHSCHEDULE_QUERY_BY_DATE = "BizCoachScheduleQueryByDate";
        private const string DATATYPE_COACHSCHEDULE_QUERY_BY_COACH = "BizCoachScheduleQueryByCoach";

        //private Business_CoachSchedule _dataDefine;

        public DataNode_BusinessCoachSchedule(string taskGuid, IDataSourceLayer dataLayer, IDataService dataService)
            : base(taskGuid, dataLayer, dataService)
        {
            _dataTypeList[DataTypeCategory.GET_DATA_LIST] = new string[] { DATATYPE_COACHSCHEDULE_INFO };
            _dataTypeList[DataTypeCategory.GET_DATA_LIST] = new string[] { DATATYPE_COACHSCHEDULE_INFO };
            _dataTypeList[DataTypeCategory.TRANSFORM_LIST] = new string[] { DATATYPE_COACHSCHEDULE_QUERY_BY_DATE, DATATYPE_COACHSCHEDULE_QUERY_BY_COACH };
            _dataDefine = new BusinessCoachScheduleInfo(dataLayer);
        }

        #region GetData
        public override XmlNode GetData(OUDataInfo UserInfo, string DataGuid, string DataType)
        {
            XmlNode ResultInfo = null;
            switch (DataType)
            {
                case DATATYPE_COACHSCHEDULE_INFO:
                    ResultInfo = GetCoachScheduleInfo(UserInfo, DataGuid);
                    break;
            }
            return ResultInfo;
        }
        #endregion

        #region SetData
        public override int SetData(OUDataInfo UserInfo, string DataGuid, string DataType, System.Xml.XmlNode XmlData)
        {
            int nResult = 1001;
            switch (DataType)
            {
                case DATATYPE_COACHSCHEDULE_INFO:
                    nResult = SetCoachScheduleInfo(UserInfo, DataGuid, XmlData);
                    break;
            }
            return nResult;
        }
        #endregion

        #region TransformData
        public override XmlNode TransformData(OUDataInfo UserInfo, string DataType, XmlNode XmlParam)
        {
            XmlNode ResultInfo = null;
            switch (DataType)
            {
                case DATATYPE_COACHSCHEDULE_QUERY_BY_DATE:
                    ResultInfo = GetCoachScheduleListByDate(UserInfo, XmlParam);
                    break;
                case DATATYPE_COACHSCHEDULE_QUERY_BY_COACH:
                    ResultInfo = GetCoachScheduleListByCoach(UserInfo, XmlParam);
                    break;
            }

            return ResultInfo;
        }
        #endregion

        #region 教练工作日信息

        //<Document TaskGuid="{智慧驾培学事管理系统任务标识}" DataGuid="{教练工作日信息标识}" DataType="BizCoachScheduleInfo">
        //    <ScheduleGuid Type="GUID" Mandatory="是">{教练工作日信息标识}</ScheduleGuid>
        //    <CoachGuid Type="GUID" Mandatory="是" Length="36">{教练标识}</CoachGuid>
        //    <CoachName Type="TEXT" Mandatory="是" Length="10">{教练姓名}</CoachName>
        //    <CoachCode Type="TEXT" Mandatory="否" Length="10">{教练工号}</CoachCode>
        //    <ScheduleDate Type="DATE" Mandatory="是" Length="8">{计划日期}</ScheduleDate>
        //    <IsWorkingDay Type="TEXT" Mandatory="是" Length="1">{是否是工作日[T:工作日|F:休息日]}</IsWorkingDay>
        //    <ScheduleMemo Type="TEXT" Mandatory="否" Length="0">{计划备注}</ScheduleMemo>
        //</Document>
        protected int SetCoachScheduleInfo(OUDataInfo UserInfo, string CoachScheduleGuid, XmlNode XmlData)
        {
            //if (!CheckPrivilege(UserInfo, "操作成员"))
            //    return -3;

            IDBDataObject InfoData = (_dataDefine as BusinessCoachScheduleInfo).GetCoachScheduleInfoData(CoachScheduleGuid);

            int nResult = 0;

            InfoData["fScheduleGuid"] = XmlNodeManage.GetSubValue(XmlData, "ScheduleGuid");
            InfoData["fSchoolGuid"] = XmlNodeManage.GetSubValue(XmlData, "SchoolGuid");
            InfoData["fSchoolName"] = XmlNodeManage.GetSubValue(XmlData, "SchoolName");
            InfoData["fCoachGuid"] = XmlNodeManage.GetSubValue(XmlData, "CoachGuid");
            InfoData["fCoachName"] = XmlNodeManage.GetSubValue(XmlData, "CoachName");
            InfoData["fCoachCode"] = XmlNodeManage.GetSubValue(XmlData, "CoachCode");
            InfoData["fScheduleDate"] = XmlNodeManage.GetSubValue(XmlData, "ScheduleDate");
            InfoData["fIsWorkingDay"] = XmlNodeManage.GetSubValue(XmlData, "IsWorkingDay");
            InfoData["fScheduleMemo"] = XmlNodeManage.GetSubValue(XmlData, "ScheduleMemo");

            nResult = InfoData.SaveData() ? 0 : 1;
            return nResult;
        }

        protected XmlNode GetCoachScheduleInfo(OUDataInfo UserInfo, string CoachScheduleGuid)
        {
            //if (!CheckPrivilege(UserInfo, "操作成员"))
            //    return null;

            IDBDataObject InfoData = (_dataDefine as BusinessCoachScheduleInfo).GetCoachScheduleInfoData(CoachScheduleGuid);
            //string SenderGuid = NotesInfoData["fSenderGuid"] as string;
            XmlNode xmlNotesInfoData = XmlNodeManage.GetDocumentNode(_taskGuid, CoachScheduleGuid, DATATYPE_COACHSCHEDULE_INFO);

            XmlNodeManage.SetSubValue(xmlNotesInfoData, "ScheduleGuid", "GUID", InfoData["fScheduleGuid"]);
            XmlNodeManage.SetSubValue(xmlNotesInfoData, "SchoolGuid", "GUID", InfoData["fSchoolGuid"]);
            XmlNodeManage.SetSubValue(xmlNotesInfoData, "SchoolName", "TEXT", InfoData["fSchoolName"]);
            XmlNodeManage.SetSubValue(xmlNotesInfoData, "CoachGuid", "GUID", InfoData["fCoachGuid"]);
            XmlNodeManage.SetSubValue(xmlNotesInfoData, "CoachName", "TEXT", InfoData["fCoachName"]);
            XmlNodeManage.SetSubValue(xmlNotesInfoData, "CoachCode", "TEXT", InfoData["fCoachCode"]);
            XmlNodeManage.SetSubValue(xmlNotesInfoData, "ScheduleDate", "DATE", InfoData["fScheduleDate"]);
            XmlNodeManage.SetSubValue(xmlNotesInfoData, "IsWorkingDay", "TEXT", InfoData["fIsWorkingDay"]);
            XmlNodeManage.SetSubValue(xmlNotesInfoData, "ScheduleMemo", "TEXT", InfoData["fScheduleMemo"]);

            return xmlNotesInfoData;
        }

        ///<summary>
        ///应用标识：智慧驾培学事管理系统 
        ///模块分类：驾校运营 
        ///功能分类：教练管理 
        ///分类序号：1102 
        ///数据编号：ZHJP-BUSINESS-COACH-1102 
        ///数据名称：按日期查询教练工作与否 
        ///数据代码：BizCoachScheduleInfoQueryByDate 
        ///数据说明：根据日期查询教练是否上班 
        /// </summary>
        ///<Document TaskGuid="{智慧驾培学事管理系统任务标识}" DataGuid="{按日期查询所有教练工作与否标识}" DataType="BizCoachScheduleQueryByDate">
        ///<PageIndex Type="LONG" Mandatory="是" Length="8">{查询页码}</PageIndex>
        ///<PageSize Type="LONG" Mandatory="是" Length="8">{查询页面大小}</PageSize>
        ///<ScheduleDate Type="DATE" Mandatory="是" Length="8">{计划日期}</ScheduleDate>
        ///</Document>
        ///<Document TaskGuid="{智慧驾培学事管理系统任务标识}" DataGuid="{按日期查询所有教练工作与否标识}" DataType="BizCoachScheduleQueryByDate">
        ///<RecordCount Type="LONG" Mandatory="是" Length="8">{查询结果总记录数}</RecordCount>
        ///<PageSize Type="LONG" Mandatory="是" Length="8">{实际返回页面大小}</PageSize>
        ///<PageIndex Type="LONG" Mandatory="是" Length="8">{实际返回页码}</PageIndex>
        ///<ScheduleList>
        ///<ScheduleDate Type="DATE" Mandatory="是" Length="8">{计划日期}</ScheduleDate>
        ///<ScheduleInfo>
        ///<ScheduleGuid Type="GUID" Mandatory="是" Length="36">{计划标识}</ScheduleGuid>
        ///<CoachGuid Type="GUID" Mandatory="是" Length="36">{教练标识}</CoachGuid>
        ///<CoachName Type="TEXT" Mandatory="是" Length="10">{教练姓名}</CoachName>
        ///<CoachCode Type="TEXT" Mandatory="否" Length="10">{教练工号}</CoachCode>
        ///<IsWorkingDay Type="TEXT" Mandatory="是" Length="1">{是否是工作日[T:工作日|F:休息日]}</IsWorkingDay>
        ///<ScheduleMemo Type="TEXT" Mandatory="否" Length="0">{计划备注}</ScheduleMemo>
        ///</ScheduleInfo>
        ///<!-- 重复ScheduleInfo -->
        ///</ScheduleList>
        ///</Document>
        protected XmlNode GetCoachScheduleListByDate(OUDataInfo UserInfo, XmlNode XmlParam)
        {
            //Generate query filter string,
            QueryFilter strQueryFilter = new QueryFilter();
            string queryScheduleDate = XmlNodeManage.GetSubValue(XmlParam, "ScheduleDate");
            string queryPageIndex = XmlNodeManage.GetSubValue(XmlParam, "PageIndex");
            string queryPageSize = XmlNodeManage.GetSubValue(XmlParam, "PageSize");
            int totalCount = 0;

            DataSet coachScheduleList = (_dataDefine as BusinessCoachScheduleInfo).GetCoachScheduleViewByScheduleDate(queryScheduleDate);

            //Pagination dataPage = new Pagination(coachScheduleList);
            //List<IDBDataObject> resultList = dataPage.GetPageDataSet(queryPageIndex, queryPageSize);
            //queryPageIndex = dataPage.PageIndex.ToString();
            //queryPageSize = dataPage.PageSize.ToString();

            if (coachScheduleList != null && coachScheduleList.Tables[0] != null)
            {
                totalCount = coachScheduleList.Tables[0].Rows.Count;
            }

            XmlNode xmlQueryData = XmlNodeManage.GetDocumentNode(_taskGuid, "", DATATYPE_COACHSCHEDULE_QUERY_BY_DATE);
            //Add the paging nodes.
            XmlNodeManage.SetSubValue(xmlQueryData, "RecordCount", "LONG", totalCount);
            XmlNodeManage.SetSubValue(xmlQueryData, "PageSize", "LONG", queryPageSize);
            XmlNodeManage.SetSubValue(xmlQueryData, "PageIndex", "LONG", queryPageIndex);
            XmlNode xmlRoot = XmlNodeManage.CreateNode("ScheduleList");
            XmlNode subNode;

            XmlNodeManage.SetSubValue(xmlRoot, "ScheduleDate", "DATE", queryScheduleDate);
            // xmlRoot.AppendChild(scheduleDateNode);

            if (totalCount > 0)
            {
                foreach (DataRow coachScheduleInfo in coachScheduleList.Tables[0].Rows)
                {
                    subNode = XmlNodeManage.CreateNode("ScheduleInfo");
                    XmlNodeManage.SetSubValue(subNode, "ScheduleGuid", "GUID", coachScheduleInfo["fScheduleGuid"]);
                    XmlNodeManage.SetSubValue(subNode, "CoachGuid", "GUID", coachScheduleInfo["fCoachGuid"]);
                    XmlNodeManage.SetSubValue(subNode, "CoachName", "TEXT", coachScheduleInfo["fCoachName"]);
                    XmlNodeManage.SetSubValue(subNode, "CoachCode", "TEXT", coachScheduleInfo["fCoachCode"]);
                    XmlNodeManage.SetSubValue(subNode, "IsWorkingDay", "TEXT", coachScheduleInfo["fIsWorkingDay"]);
                    XmlNodeManage.SetSubValue(subNode, "ScheduleMemo", "TEXT", coachScheduleInfo["fScheduleMemo"]);

                    xmlRoot.AppendChild(subNode);
                }
            }
            xmlQueryData.AppendChild(xmlRoot);
            return xmlQueryData;
        }

        //应用标识：智慧驾培学事管理系统 
        //模块分类：驾校运营 
        //功能分类：教练管理 
        //分类序号：1103 
        //数据编号：ZHJP-BUSINESS-COACH-1103  
        //数据名称：按教练查询工作情况 
        //数据代码：BizCoachScheduleInfoQueryByCoach 
        //数据说明：按教练查询一段日期内教练的上班情况 
        //
        //---------------- 查询条件 数据规范---------------------
        //<Document TaskGuid="{智慧驾培学事管理系统任务标识}" DataGuid="{按教练查询工作情况标识}" DataType="BizCoachScheduleInfoQueryByCoach">
        //    <PageIndex Type="LONG" Mandatory="是" Length="8">{查询页码}</PageIndex>
        //    <PageSize Type="LONG" Mandatory="是" Length="8">{查询页面大小}</PageSize>
        //    <CoachGuid Type="GUID" Mandatory="是" Length="36">{教练标识}</CoachGuid>
        //    <ScheduleStart Type="DATE" Mandatory="" Length="8">{开始日期}</ScheduleStart>
        //    <ScheduleEnd Type="DATE" Mandatory="" Length="8">{结束日期}</ScheduleEnd>
        //</Document>
        //
        //---------------- 查询返回 数据规范---------------------
        //<Document TaskGuid="{智慧驾培学事管理系统任务标识}" DataGuid="{按教练查询工作情况标识}" DataType="BizCoachScheduleInfoQueryByCoach">
        //    <RecordCount Type="LONG" Mandatory="是" Length="8">{查询结果总记录数}</RecordCount>
        //    <PageSize Type="LONG" Mandatory="是" Length="8">{实际返回页面大小}</PageSize>
        //    <PageIndex Type="LONG" Mandatory="是" Length="8">{实际返回页码}</PageIndex>
        //    <ScheduleList>
        //        <CoachGuid Type="GUID" Mandatory="是" Length="36">{教练标识}</CoachGuid>
        //        <CoachName Type="TEXT" Mandatory="是" Length="10">{教练姓名}</CoachName>
        //        <CoachCode Type="TEXT" Mandatory="否" Length="10">{教练工号}</CoachCode>
        //        <ScheduleInfo>
        //            <ScheduleGuid Type="GUID" Mandatory="是" Length="36">{计划标识}</ScheduleGuid>
        //            <ScheduleDate Type="DATE" Mandatory="是" Length="8">{计划日期}</ScheduleDate>
        //            <IsWorkingDay Type="TEXT" Mandatory="是" Length="1">{是否是工作日[T:工作日|F:休息日]}</IsWorkingDay>
        //            <ScheduleMemo Type="TEXT" Mandatory="否" Length="0">{计划备注}</ScheduleMemo>
        //        </ScheduleInfo>
        //        <!-- 重复ScheduleInfo -->
        //    </ScheduleList>
        //</Document>
        protected XmlNode GetCoachScheduleListByCoach(OUDataInfo UserInfo, XmlNode XmlParam)
        {
            //Generate query filter string,
            QueryFilter strQueryFilter = new QueryFilter();
            string scheduleStart = XmlNodeManage.GetSubValue(XmlParam, "ScheduleStart");
            string scheduleEnd = XmlNodeManage.GetSubValue(XmlParam, "ScheduleEnd");
            string queryPageIndex = XmlNodeManage.GetSubValue(XmlParam, "PageIndex");
            string queryPageSize = XmlNodeManage.GetSubValue(XmlParam, "PageSize");
    
            if (!string.IsNullOrEmpty(scheduleStart) && string.IsNullOrEmpty(scheduleEnd))
            {
                strQueryFilter.AddFilter("fScheduleDate", scheduleStart, "{0} >= '{1}'");
            }
            else if (string.IsNullOrEmpty(scheduleStart) && !string.IsNullOrEmpty(scheduleEnd))
            {
                strQueryFilter.AddFilter("fScheduleDate", scheduleEnd, "{0} <= '{1}'");
            }
            else
            {
                strQueryFilter.AddFilter("fScheduleDate", "'" + scheduleStart + "' AND '" + scheduleEnd +"'", "{0} BETWEEN {1} ", false);
            }

            IDBBigObjectSet coachScheduleList = (_dataDefine as BusinessCoachScheduleInfo).GetCoachScheduleInfoListByFilter(strQueryFilter.FilterString);

            Pagination dataPage = new Pagination(coachScheduleList);
            List<IDBDataObject> resultList = dataPage.GetPageDataSet(queryPageIndex, queryPageSize);
            queryPageIndex = dataPage.PageIndex.ToString();
            queryPageSize = dataPage.PageSize.ToString();


            XmlNode xmlQueryData = XmlNodeManage.GetDocumentNode(_taskGuid, "", DATATYPE_COACHSCHEDULE_QUERY_BY_DATE);
            
            XmlNodeManage.SetSubValue(xmlQueryData, "RecordCount", "LONG", coachScheduleList.DataCount);
            XmlNodeManage.SetSubValue(xmlQueryData, "PageSize", "LONG", queryPageSize);
            XmlNodeManage.SetSubValue(xmlQueryData, "PageIndex", "LONG", queryPageIndex);

            XmlNode xmlRoot = XmlNodeManage.CreateNode("ScheduleList");
            XmlNode subNode;

            XmlNodeManage.SetSubValue(xmlRoot, "CoachGuid", "GUID", coachScheduleList["fCoachGuid"]);
            XmlNodeManage.SetSubValue(xmlRoot, "CoachName", "TEXT", coachScheduleList["fCoachName"]);
            XmlNodeManage.SetSubValue(xmlRoot, "CoachCode", "TEXT", coachScheduleList["fCoachCode"]);

            bool isFirstRecord = true;
            foreach (IDBDataObject coachScheduleInfo in resultList)
            {
                if (isFirstRecord)
                {
                    XmlNodeManage.SetSubValue(xmlRoot, "CoachGuid", "GUID", coachScheduleInfo["fCoachGuid"]);
                    XmlNodeManage.SetSubValue(xmlRoot, "CoachName", "TEXT", coachScheduleInfo["fCoachName"]);
                    XmlNodeManage.SetSubValue(xmlRoot, "CoachCode", "TEXT", coachScheduleInfo["fCoachCode"]);
                }
                subNode = XmlNodeManage.CreateNode("ScheduleInfo");
                XmlNodeManage.SetSubValue(subNode, "ScheduleGuid", "GUID", coachScheduleInfo["fScheduleGuid"]);
                XmlNodeManage.SetSubValue(subNode, "ScheduleDate", "DATE", coachScheduleInfo["fScheduleDate"]);
                XmlNodeManage.SetSubValue(subNode, "IsWorkingDay", "TEXT", coachScheduleInfo["fIsWorkingDay"]);
                XmlNodeManage.SetSubValue(subNode, "ScheduleMemo", "TEXT", coachScheduleInfo["fScheduleMemo"]);

                xmlRoot.AppendChild(subNode);
                isFirstRecord = false;
            }
            xmlQueryData.AppendChild(xmlRoot);
            return xmlQueryData;
        }
        #endregion

    }
}
